/**
 * 
 */
package com.tio.live.main.controller;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.tio.live.bs.api.Const;
import org.tio.live.bs.utils.PeriodUtils;

import com.tio.live.main.model.db02.User;
import com.tio.live.main.model.db02.UserExample;
import com.tio.live.main.model.db02.vo.UserVo;
import com.tio.live.main.service.UserServiceLocal;
import com.tio.manager.core.pager.Pageable;
import com.tio.manager.web.utils.WebUtils;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;


/**
 * 用户表
 * 
 * @author: xufei
 */
@Controller
@RequestMapping("/user/*")
public class UserController
{
	private static Logger log = LoggerFactory.getLogger(UserController.class);

	/**
	 * 
	 */
	public UserController()
	{

	}

	@RequestMapping(value = "/init")
	public String init(UserVo userVo)
	{
		return "user/userInit";
	}
	@RequestMapping(value = "/rechargeInit")
	public String rechargeInit(UserVo userVo)
	{
		return "user_recharge/userInit";
	}
	@RequestMapping(value = "/robotPInit")
	public String robotPInit(UserVo userVo)
	{
		return "user/robotInit";
	}

	@RequestMapping(value = "/robotInit")
	public String robotInit(UserVo userVo)
	{
		return "robot/robotInit";
	}

	@RequestMapping(value = "/add")
	public void add(UserVo userVo, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		try 
		{
			UserServiceLocal.add(userVo);
			WebUtils.writeSuccess(WebUtils.getI18NMessage("talent.add.success", null, "record add success"), null, request, response);
		} catch (Exception e)
		{
			log.error(e.toString(), e);
			WebUtils.writeFail(e, request, response);
		}
	}
	
	@RequestMapping(value = "/delete")
	public void delete(UserVo userVo, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		try 
		{
			int deletedCount = UserServiceLocal.delete(userVo);
			if (deletedCount < 1) 
			{
				WebUtils.writeFail("删除失败!", request, response);
			} else 
			{
				WebUtils.writeSuccess("删除成功!", null, request, response);
			}
		} catch (Exception e) 
		{
			log.error(e.toString(), e);
			WebUtils.writeFail(e, request, response);
		}
	}

	/**
	 * 批量删除
	 * @param userVo
	 * @param ids
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/batchDelete")
	public void batchDelete(UserVo userVo, Integer[] ids, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		try 
		{
			if (ids == null || ids.length == 0)
			{
				WebUtils.writeFail("删除失败!", request, response);
				return;
			}
			
			int deletedCount = UserServiceLocal.batchDelete(ids);
			
			if (deletedCount < 1) 
			{
				WebUtils.writeFail("删除失败!", request, response);
			} else if (deletedCount == ids.length) 
			{
				WebUtils.writeSuccess("删除成功!", null, request, response);
			} else 
			{
				WebUtils.writeFail("删除失败!", request, response);
			}
		} catch (Exception e) 
		{
			log.error(e.toString(), e);
			WebUtils.writeFail(e, request, response);
		}
	}

	/**
	 * 更新记录
	 * @param userVo
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/update")
	public void update(UserVo userVo, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		try 
		{
			int updateCount = UserServiceLocal.updateByPrimaryKeySelective(userVo);
			if (updateCount < 1) 
			{
				WebUtils.writeFail("更新失败!", request, response);
			} else 
			{
				WebUtils.writeSuccess("更新成功!", null, request, response);
			}
		} catch (Exception e) 
		{
			log.error(e.toString(), e);
			WebUtils.writeFail(e, request, response);
		}
	}

	/**
	 * 分页查询
	 * @param pageSize
	 * @param pageIndex
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/pageQuery")
	public void pageQuery(Integer pageSize, Integer pageIndex, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		try 
		{
			Map<String, Object> params = WebUtils.fillParams(request, null);
			Pageable page = UserServiceLocal.pageQuery(pageSize, pageIndex, params);
			WebUtils.writeSuccess(page, request, response); 
		} catch (Exception e) 
		{
			log.error(e.toString(), e);
			WebUtils.writeFail(e, request, response);
		}
	}

	@RequestMapping(value = "/selectById")
	public void selectById(Integer id, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		try 
		{
			User data = UserServiceLocal.selectById(id);
			WebUtils.writeSuccess(data, request, response); 
		} catch (Exception e) 
		{
			log.error(e.toString(), e);
			WebUtils.writeFail(e, request, response);
		}
	}

	/**
	 * 主函数
	 * @param args
	 */
	public static void main(String[] args)
	{
	
	}
}
